package com.project.class8_25;

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {
    public Client(){
        try {
            //创建socket对象，连接指定IP和端口的服务器
            Socket socket = new Socket("localhost",8088);
            //从socket中，得到读取流，用于接收服务器发送的数据
            InputStream in = socket.getInputStream();
            //从socket中，得到写入流，用于将数据发送给服务器
            OutputStream out = socket.getOutputStream();

            String str = JOptionPane.showInputDialog(null,"请输入发送信息");
            //向服务器发送数据
            out.write(str.getBytes());
            out.flush();

            //接收服务器数据
            byte[] byteArray = new byte[1024];
            in.read(byteArray);

            String info = new String(byteArray).trim();
            JOptionPane.showMessageDialog(null,info);

            //关闭流
            in.close();
            out.close();
            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new Client();
    }
}
